package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.pojo.Shelves;
import com.ccc.demoboot.service.ShelvesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author wz
 * @Date 2024/6/4 10:32
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: ShelvesController
 */
@RequestMapping("/shelves")
@RestController
public class ShelvesController {
    @Autowired
    ShelvesService shelvesService;

//    /**
//     * 分页查询所有仓库区货架
//     * @param page
//     * @return
//     */
//    @PostMapping("/select/{page}")
//    public R select(@PathVariable("page") int page) {
//        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
//        List<Map<String,Object>> list=this.shelvesService.selectShelves();
//        PageInfo<Map<String,Object>> menuPageInfo = new PageInfo<>(list);
//        return new R().add("pageInfo", menuPageInfo);
//    }

    /**
     * 查询所有货架（包含所属父类）
     * @return
     */
    @PostMapping("/selecthelves/{page}")
    public R selectShelves(@PathVariable("page") int page){
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Map<String,Object>> list=this.shelvesService.selectShelves();
        PageInfo<Map<String,Object>> menuPageInfo = new PageInfo<>(list);
        return new R().add("pageInfoA", menuPageInfo);
//        List<Shelves> list = this.shelvesService.list();
//        return new R().add("list",list);
    }


    /**
     * 根据条件查询货架
     * @param
     * @return
     */
    @PostMapping("/shelvesSelect")
    public R shelvesSelect(@RequestBody Map<String,Object> map){
        List<Map<String,Object>> list = this.shelvesService.shelvesSelect(map.get("shelves")+"");
        return new R().add("list",list);

    }

    /**
     * 添加新的仓库区货架
     * @param shelves
     * @return
     */
    @PostMapping("/addShelves")
    public R addShelves(@RequestBody Shelves shelves) {
        boolean save = this.shelvesService.save(shelves);
        return new R().add("save", save);
    }


    /**
     * 修改仓库区货架信息
     * @param shelves
     * @return
     */
    @PostMapping("/upShelves")
    public R upShelves(@RequestBody Shelves shelves) {
        boolean update = this.shelvesService.updateById(shelves);
        return new R().add("update", update);
    }

    /**
     * 根据ID查询该仓库区货架的一条信息
     * @param shelves
     * @return
     */
    @PostMapping("/seloneid")
    public R selOneid(@RequestBody Shelves shelves) {
        Shelves byId = this.shelvesService.getById(shelves.getId());
        return new R().add("oneid", byId);
    }

    /**
     * 根据id删除仓库货架，如果有引用则无法删除
     * @param shelves
     * @return
     */
    @PostMapping("/delShelves")
    public R delShelves(@RequestBody Shelves shelves){
        QueryWrapper<Shelves> shelvesQueryWrapper = new QueryWrapper<>();
        shelvesQueryWrapper.setEntity(shelves);
        boolean remove = this.shelvesService.remove(shelvesQueryWrapper);
        return new R().add("remove",remove);
    }

}
